package qj.app.trader;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import qj.tool.string.StringExtractor;
import qj.util.Cols;
import qj.util.DebugUtil;
import qj.util.StringUtil;

public class TraderMain {
	public static void main(String[] args) {
//		WebSession webSession = new WebSession();
//		webSession.cookies.put("orderby", "3");
//		String content = webSession.getString("http://www.bonbanh.com/oto/Hyundai-Santa_Fe/page,3/");
//		DebugUtil.save(content);
		
		String content = DebugUtil.load();
//		System.out.println(content);
		StringExtractor stringExtractor = new StringExtractor(content);
		
		String itemContent = stringExtractor.nextBetween("<div class=\"car-item", "<!-- end car");
		System.out.println(parseItem(itemContent));
	}

	private static Map<String, Object> parseItem(String itemContent) {
		System.out.println(itemContent);
		
		
		// Get year
		String yearStr = StringExtractor.nextBetween("<div class=\"cb1\">", "</div>", itemContent);
		String year = StringExtractor.nextBetween("<b>", "</b>", yearStr);
		
		// Get title
		String titleAreaStr = StringExtractor.nextBetween("<div class=\"cb2_02\">", "</div>", itemContent);
		String title = StringUtil.trimDeep(StringExtractor
				.nextBetween("<a title=\"", "\" href=", titleAreaStr));
		String href = StringUtil.trimDeep(StringExtractor
				.nextBetween("href=\"", "\"><b>", titleAreaStr));
		
		// Get price
		String priceStr = StringExtractor.nextBetween("<div class=\"cb3\">", "</div>", itemContent);
		String priceDisplay = StringExtractor.nextBetween("<b>", "<span style=", priceStr).trim();
		String priceUnit = StringUtil.getLastWord(priceDisplay);
		BigDecimal priceValue = new BigDecimal(priceDisplay.replaceAll(priceUnit, "").trim());
		
		// Get code n image
		String codeNImageStr = StringExtractor.nextBetween("<div class=\"cb5\">", "</div>", itemContent);
		String code = StringExtractor.nextBetween("<span class='car_code'>Mã: ", "</span>", codeNImageStr);
		String imgUrl = StringExtractor.nextBetween("<img src=\"", "\" class=\"h-car-img\">", codeNImageStr);
		
		// Get contact
		String contactStr = StringExtractor.nextBetween("<div class=\"cb7\">", "</div>", itemContent);
		StringExtractor contactExtractor = new StringExtractor(contactStr);
		String contactName = contactExtractor.nextBetween("<b>", "</b><br>");
		String contactAddress = contactExtractor.untilNext("<br><br>ĐT:").trim();
		List<String> contactPhones = Arrays.asList(contactExtractor.toEnd().trim().split("-"));
		return Cols.map(
				"title", title,
				"href", href,
				"priceDisplay", priceDisplay,
				"priceUnit", priceUnit,
				"priceValue", priceValue,
				"code", code,
				"imageUrl", imgUrl,
				"contactName", contactName,
				"contactAddress", contactAddress,
				"contactPhones", contactPhones,
				"year", year
				);
	}
	
//	public static class Deployer {
//		public static void main(String[] args) {
//			DeployUtil.deploy(TraderMain.class, "localhost:1213");
//		}
//	}
}
